package cn.csg.iotplatform.module.system.aspects;

import cn.csg.iotplatform.framework.common.util.validation.ValidationUtils;
import cn.csg.iotplatform.module.system.controller.admin.user.vo.user.UserSaveReqVO;
import cn.csg.iotplatform.module.system.service.user.AdminUserService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Aspect
@Component
@Order(1)
public class DecryptDesensitizationAspect {

    @Resource
    private AdminUserService adminUserService;

    @Before("@annotation(cn.csg.iotplatform.module.system.annotations.DecryptDesensitization)")
    public void doBefore(JoinPoint point)
    {
        decryptDesensitization(point);
    }

    private void decryptDesensitization(JoinPoint point) {
        Object object = point.getArgs()[0];
        if(object instanceof UserSaveReqVO){
            UserSaveReqVO userSaveReqVO = (UserSaveReqVO) object;
            adminUserService.validateDesensitization(userSaveReqVO,false);
            ValidationUtils.validate(userSaveReqVO);
        }
    }

}